package com.scarecrow.olga.test {
	
	import com.scarecrow.olga.entity.Entity;
	import com.scarecrow.olga.entity.EntityRegistry;
	import com.scarecrow.olga.input.event.KeyboardInputEvent;
	import com.scarecrow.olga.input.InputManager;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.system.System;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class EntityStressTest extends Sprite {
		
		private var ids:Array = [];
		private var mem:Number;
		private var pepeManager:InputManager;
		
		private var acumInstanceTime:uint;
		
		public function EntityStressTest():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			
			stage.addEventListener(Event.ENTER_FRAME, onEF);
			mem = System.totalMemory / 1024;
			for (var i:int = 0; i < 1250; i++) {
				var pp:Entity = new Entity();
				ids.push(pp.id);
			}
			pepeManager = new InputManager();
			pepeManager.stage = this.stage;
			pepeManager.messageBroadcaster.addEventListener(KeyboardInputEvent.KEY_UP, onKUp);
			pepeManager.messageBroadcaster.addEventListener(KeyboardInputEvent.KEY_DOWN, onKDn);
		}
		
		private function onKDn(e:KeyboardInputEvent):void {
			var start:uint = getTimer();
			var pp:Entity = new Entity();
			ids.push(pp.id);
			acumInstanceTime += getTimer() - start;
		}
		
		private function onKUp(e:KeyboardInputEvent):void {
			/*for (var i:int = 0; i < 1250; i++) {
				var pp:Entity = new Entity();
				ids.push(pp.id);
			}*/
		}
		
		private function onEF(e:Event):void {
			/*for each (var item:String in ids) {
				var ent:Entity = EntityRegistry.getEntity(item) as Entity;
				if (ent) ent.update(0);
			}*/
			trace("ents:[" + EntityRegistry.entityCount + "], instance time:[" + acumInstanceTime + "]");
			trace("MEM: " + ((System.totalMemory/1024)-mem) + "kb(" + Math.round((((System.totalMemory)-mem*1024)/EntityRegistry.entityCount)) + " bytes per entity)")
		}
	}
}